<?php
/**
 * 
 * @author HZ_02
 * 会员卡实体类
 */
class VipcardAction extends CommonAction{
	
	/**
	 * 显示添加会员卡
	 * 添加会员卡处理
	 */
	public function vip_add(){
		
		$model   = D("Actvipcard");
		$vipuser = M("ActvipcardUser"); 
		
		if(IS_POST){
			if($vipuser->create()){
				$YYYY = $this->_param('YYYY');
				$MM   = $this->_param('MM');
				$DD   = $this->_param('DD');
				$vipuser->birthday = $YYYY."-".$MM."-".$DD;
				
				if(empty($vipuser->vipid)){
					$this->error(L('vcd_form_ele_activityid'));
				}
				
				$data = $vipuser->data();
				$json = sendRest("Vip/get",$data);
				
				if($json['status']==1){
					$this->success(L('vcd_form_ele_addsucc'),U('Vipcard/vip_list'));
				}else{
					$this->error($json['info']);
				}
			}else{
				$this->error(L('vcd_form_ele_addfail'));
			}
		}else{
			$this->display();	
		}
	}
	
	/**
	 * 会员卡电话号码匹配
	 */
	public function vip_ship(){
		if(IS_POST){
			$mobile = $this->_param("mobile");
			if(empty($mobile)){
				$this->error(L('vcd_form_ele_telnotempty'));
			}else{
				$map['mobile'] = $mobile;
				$json = sendRest("Vip/view",$map);
				
				if($json['data']['id']){
					$this->assign("jumpUrl",U('/Vipcard/vip_use','mobile='.$mobile));   
					$this->success(L('vcd_form_ele_successmatch'));	
				}else{
					$this->error(L('vcd_form_ele_failmatch'));
				}	
			}
		}else{
			$this->display();	
		}	
	}
	
	/**
	 * 显示使用会员卡页面
	 * 处理使用会员卡
	 */
	public function vip_use(){
		if(IS_POST){
			$model  = D("Actvipcard");
			$act    = $this->_param("act");   //消费类型
			$userid = $this->_param("userid");//会员id 
			
			if(empty($userid)){
				$this->error("会员卡消费失败,请先注册成为商城会员");
			}
			
			if($act=="plus"||$act=="minus"){
				//积分操作记录
				$map  = $model->getVipCreditArr();
				$json = sendRest("Vip/addcredit",$map);
			}else{
				//金额操作记录
				$map  = $model->getVipMoneyArr();
				$json = sendRest("Vip/addmoney",$map);
			}
			
			if($json['status']==1){
				$this->success("会员卡消费成功",U('Vipcard/vip_ship'));
			}else{
				$this->error("会员卡消费失败");
			}
		}else{
			
			$mobile = $this->_param("mobile");
			
			$map['mobile'] = $mobile;
			$json = sendRest("Vip/view",$map); 
			
			$this->assign("detail",$json);
			$this->display();	
		}	
	}
	
	/**
	 * 会员卡管理列表
	 * 删除会员卡
	 */
	public function vip_list(){
		if(IS_AJAX){
			$map['id'] = $this->_param('id');
			$json = sendRest("Vip/del", $map);
			$this->ajaxReturn($json);
		}else{
			$json = sendRest("Vip/viplist");
			
			foreach($json['data'] as $k=>$v){
				$json['data'][$k]['sex'] = $v['sex']==1?"男":"女";
			}
			
			$this->assign("list",$json);
			$this->display();
		}
	}
	
	/**
	 * 会员卡统计
	 */
	public function vip_total(){
		$this->display();
	}
	
	
	/**
	 * 会员卡详情
	 */
	public function vip_detail(){
		$mobile = $this->_param("id");
		
		$map['mobile'] = $mobile;
		$json = sendRest("Vip/view",$map);
		
		$this->assign("detail",$json);
		$this->display();
	}
}
?>